package com.lb.function;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author: zhangjun
 * @date: 2024/10/11
 */
public class FuncTest {
    private static final ThreadLocal<SimpleDateFormat> formatterThreadLocal =
            ThreadLocal.withInitial(() -> new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH));

    private static String format(Date date) {
        return formatterThreadLocal.get().format(date);
    }

    public static void main(String[] args) {
        // 判断函数
        Predicate<Integer> pre = x -> x > 5;
        System.out.println(pre.test(11));

        // 操作函数
        BinaryOperator<Long> opt = Long::sum;
        System.out.println(opt.apply(4L, 5L));

        // 输入-输出转换函数
        Function<Long, Long> function = x -> x + 1;
        System.out.println(function.apply(5L));

        // 线程安全地格式化日期
        System.out.println(format(new Date()));

        int count = Stream.of(1,2,3).reduce(0, Integer::sum);
        System.out.println(count);

        String concatStr = Stream.of("Jack Chan", "Jay", "Wang").collect(Collectors.joining(",", "[", "]"));
        System.out.println(concatStr);

        List<Album> albums = new ArrayList<Album>() {{
            add(new Album("Li", "Let's go"));
            add(new Album("Li", "Wing"));
            add(new Album("wang","Just do it"));
        }};
        Map<String, Long> albumsMap = albums.stream().collect(Collectors.groupingBy(Album::getMainMusician, Collectors.counting()));
        System.out.println(albumsMap);

        Map<String, List<String>> collect = albums.stream()
                .collect(Collectors.groupingBy(Album::getMainMusician, Collectors.mapping(Album::getName, Collectors.toList())));
        System.out.println(collect);
    }
}
